package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.entity.bus.Enterprise;
import cn.enilu.flash.bean.entity.bus.Sport;
import cn.enilu.flash.bean.entity.bus.SportAppointment;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.mobile.domain.EnterpriseDomain;
import cn.enilu.flash.service.bus.CollectionService;
import cn.enilu.flash.service.bus.EnterpriseService;
import cn.enilu.flash.service.bus.SportAppointmentService;
import cn.enilu.flash.service.bus.SportService;
import cn.enilu.flash.utils.factory.Page;
import cn.enilu.flash.web.controller.BaseController;
import jodd.util.ObjectUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;


@RestController
@RequestMapping("/sport")
public class SportController extends BaseController {

    @Autowired
    private SportService sportService;

    @Autowired
    private SportAppointmentService sportAppointmentService;

    @GetMapping(value = "/list")
    public Object list() {
        List<Sport> sports = sportService.queryAll(SearchFilter.build("state", SearchFilter.Operator.EQ, "1"));
        return Rets.success(sports);
    }

    @GetMapping(value = "/{id}")
    public Object getInfoById(@PathVariable("id") Long id) {
        Sport sport = sportService.get(id);
        return Rets.success(sport);
    }

    @GetMapping(value = "/appoint/list")
    public Object appointList() {
        Long idUser = getIdUser();
        Page<Sport> page = new PageFactory<Sport>().defaultPage();
        page.addFilter(SearchFilter.build("userId", SearchFilter.Operator.EQ,  idUser));
        page.setSort(Sort.by(Sort.Direction.DESC, "createTime"));
        page = sportService.queryPage(page);
        return Rets.success(page);
    }

    @PostMapping("/appoint")
    public Object appoint(@RequestBody SportAppointment sportAppointment) {
        Long idUser = getIdUser();
        if (null != idUser) {
            sportAppointment.setUserId(idUser);
            sportAppointment.setCreateBy(idUser);
            sportAppointment.setModifyBy(idUser);
            sportAppointment.setAppointEnd(DateUtils.addHours(sportAppointment.getAppointStart(), 1));
            SportAppointment insert = sportAppointmentService.insert(sportAppointment);
            return Rets.success(insert);
        } else {
            return Rets.failure("预约请登录");
        }
    }
}
